 import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.ResourceBundle;

public class test {
    @Test
    public void test4() throws Exception{//测试容器产生数据源对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource =  context.getBean(DataSource.class);
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }
    @Test
    public void test3() throws Exception{
        ResourceBundle rb = ResourceBundle.getBundle("jdbc");//读取配置文件
        String driver = rb.getString("jdbc.driver");
        String url = rb.getString("jdbc.url");
        String username = rb.getString("jdbc.username");
        String password = rb.getString("jdbc.password");
        ComboPooledDataSource dataSource = new ComboPooledDataSource();//c3p0连接池对象
        dataSource.setDriverClass(driver);//设置驱动
        dataSource.setJdbcUrl(url);//设置连接地址
        dataSource.setUser(username);//设置用户名
        dataSource.setPassword(password);//设置密码
        Connection connection = dataSource.getConnection();//获取连接
        System.out.println(connection);//打印连接
        connection.close();//释放资源
    }
    @Test
    public void test2() throws Exception{//druid连接池/数据源
        DruidDataSource dataSource = new DruidDataSource();//druid连接池对象
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");//设置驱动
        dataSource.setUrl("jdbc:mysql://localhost:3306/test");//设置连接地址
        dataSource.setUsername("root");//设置用户名
        dataSource.setPassword("123456");//设置密码
        Connection connection = dataSource.getConnection();//获取连接
        System.out.println(connection);//打印连接
        connection.close();//释放资源
    }
    @Test
    public void test1() throws Exception{//手动创建c3p0连接池/数据源
        ComboPooledDataSource dataSource = new ComboPooledDataSource();//c3p0连接池对象
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");//设置驱动
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");//设置连接地址
        dataSource.setUser("root");//设置用户名
        dataSource.setPassword("123456");//设置密码
        Connection connection = dataSource.getConnection();//获取连接
        System.out.println(connection);//打印连接
        connection.close();//释放资源
    }
}
